package com.wwwarehouse.common.custom_widget.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.wwwarehouse.common.R;

/**
 * @author : mundane
 * @time : 2017/8/22 19:01
 * @description :
 * @file : ConfirmDialogFragment.java
 */

public class ConfirmDialogFragment extends DialogFragment {

	private Button mBtnConfirm;
	private Button mBtnCancel;
	private TextView mTvTitle;
	private OnConfirmClickListener mOnConfirmClickListener;
	private onCancelClickListener mOnCancelClickListener;
	private CharSequence mTitle;

	public static ConfirmDialogFragment newInstance() {
		ConfirmDialogFragment dialogFragment = new ConfirmDialogFragment();
		return dialogFragment;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar);
	}

	public ConfirmDialogFragment setTitle(CharSequence title) {
		mTitle = title;
		return this;
	}

	public interface OnConfirmClickListener {
		void onConfirmClick(View view);
	}

	public interface onCancelClickListener {
		void onCancelClick(View view, DialogFragment dialog);
	}

	public ConfirmDialogFragment setOnCancelClickListener(onCancelClickListener onCancelClickListener) {
		mOnCancelClickListener = onCancelClickListener;
		return this;
	}

	public ConfirmDialogFragment setOnConfirmClickListener(OnConfirmClickListener onConfirmClickListener) {
		mOnConfirmClickListener = onConfirmClickListener;
		return this;
	}


	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		View rootView = inflater.inflate(R.layout.layout_dialog_confirm, container, false);
		//设置窗口以对话框样式显示
//		setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog);
		// 设置对话框背景色，否则有虚框
		// 这句代码十分重要, 否则圆角背景的圆角永远就只有那么大
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		getDialog().setCanceledOnTouchOutside(false);
		return rootView;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		mBtnConfirm = (Button) view.findViewById(R.id.btn_confirm);
		mBtnCancel = (Button) view.findViewById(R.id.btn_cancel);
		mTvTitle = (TextView) view.findViewById(R.id.tv_title);
		mTvTitle.setText(mTitle);
		mBtnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ConfirmDialogFragment.this.dismiss();
				if (mOnCancelClickListener != null) {
					mOnCancelClickListener.onCancelClick(v, ConfirmDialogFragment.this);
				}
			}
		});

		mBtnConfirm.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ConfirmDialogFragment.this.dismiss();
				if (mOnConfirmClickListener != null) {
					mOnConfirmClickListener.onConfirmClick(v);
				}
			}
		});
	}
}
